package com.eliza.lifecycle.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.SystemClock
import android.widget.Chronometer
import androidx.lifecycle.ProcessLifecycleOwner
import com.eliza.lifecycle.R
import com.eliza.lifecycle.newControls.NewChronometer
import com.eliza.lifecycle.observer.NewDefaultLcObserver

/*
* 高耦合
* */
class OldActivity : AppCompatActivity() {
    lateinit var chr: Chronometer
    var elapsedTime: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lifecycle_oldmain)
        /**
        自定义控件 实现生命周期解耦合
        * */
        lifecycle.addObserver(findViewById<NewChronometer>(R.id.main_cu_chro))

        chr = findViewById<Chronometer>(R.id.main_chro)



        ProcessLifecycleOwner.get().lifecycle.addObserver(
            NewDefaultLcObserver()
        )
    }

    override fun onResume() {
        super.onResume()

        chr.base = SystemClock.elapsedRealtime() - elapsedTime //开机时间
        chr.start()
    }

    override fun onPause() {
        super.onPause()
        elapsedTime = SystemClock.elapsedRealtime() - chr.base //开机时间

        chr.stop()
    }
}